/*************************************************************************/
/* Configuration parameters                                              */

var server   = "Quixote"        // your FTP server name
var userName = "Wolfgang Loch"  // your FTP account
var password = "wolo"           // your FTP password

var localFile  = "C:\\FtpUpload.html"  // a temporary local file name
var remoteFile = "FtpUpload.html"      // file name on the remote server

/*************************************************************************/

var ftp;
var currentSong = "";
var previousSong = "";
var nextSong = "";
var BLUE = 128*256*256;
var RED = 128;

// The Application::Startup event is fired on startup of Raduga
function Application::Startup()
{
    Application.Console.Visible = true;
    DebugOutput( "FtpUpload started" );
}

// The Application::Shutdown event is fired on shutdown of Raduga
function Application::Shutdown()
{
    if( ftp != null )
        ftp.Close();
}

// The Player::Start event is fired by Raduga whenever a new track starts
function Player::Start()
{
    var playlist = Application.Playlist;

    previousSong = currentSong;
    currentSong = playlist.DisplayNameOf( Player.FileName )
    nextSong = playlist.DisplayNameOf( playlist.Item( playlist.NextIndex ) )
    
    WriteFile();
    UploadFile();
}

function WriteFile()
{
    Application.Statusbar = "Writing file " + localFile;
    DebugOutput( "Writing file " + localFile );
    
    var ForWriting = 2;
    var FormatAscii = 0;
    var fso = new ActiveXObject( "Scripting.FileSystemObject" )
    fso.CreateTextFile( localFile );
    var file = fso.GetFile( localFile );
    var ts = file.OpenAsTextStream( ForWriting, FormatAscii );

    ts.WriteLine( "<HTML><HEAD>" );
    ts.WriteLine( "<META HTTP-EQUIV=\"Refresh\" Content=\"10\">" );
    ts.WriteLine( "<TITLE>Raduga</TITLE>" );
    ts.WriteLine( "<STYLE>TD { font-family: sans-serif }</STYLE>" );
    ts.WriteLine( "</HEAD>" );
    ts.WriteLine( "<BODY>" );
    ts.WriteLine( "<TABLE>" );
    ts.WriteLine( "<TR><TD>Previous:</TD><TD>" + previousSong + "</TD></TR>" );
    ts.WriteLine( "<TR><TD>On Air:</TD><TD><B>" + currentSong + "</B></TD></TR>" );
    ts.WriteLine( "<TR><TD>Next:</TD><TD>" + nextSong + "</TD></TR>" );
    ts.WriteLine( "</TABLE>" );
    ts.WriteLine( "<A HREF=\"http://www.wolosoft.com/en/raduga/\">Powered by WoLoSoft Raduga</A>" );
    ts.WriteLine( "</BODY></HTML>" );
    ts.Close();
    
    Application.Statusbar = "";
}

function UploadFile()
{
    Application.Statusbar = "Uploading file " + localFile + " to " + remoteFile;

    try
    {
        if( ftp == null )
        {
            ftp = new ActiveXObject( "Raduga.Ftp" );
            ftp.Server   = server;
            ftp.UserName = userName;
            ftp.Password = password;

            DebugOutput( "Connecting to FTP server " + ftp.Server );
            ftp.Open();
        }
        
        DebugOutput( "Uploading file " + localFile + " to " + remoteFile );
        ftp.PutFile( localFile, remoteFile );
    }
    catch( error )
    {
        HandleError( error );
        ftp = null;
    }

    Application.Statusbar = "";
}

function HandleError( error )
{
    Application.StatusBar = "FtpUpload Error: " + error.description;
    Application.Console.WriteLineColor( error.description, RED );
}

function DebugOutput( text )
{
    Application.Console.WriteLineColor( text, BLUE );
}
